package com.locationbasedagent;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.locationbasedagent.shared.HouseListingProxy;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ResultListAdapter extends BaseAdapter {
	private final Activity context;
	private final List<HouseListingProxy> listings; // = new ArrayList<HouseListingProxy>();
	 private final LayoutInflater inflater;
	
    private Comparator<HouseListingProxy> houseListingComparator = new Comparator<HouseListingProxy>() {
        public int compare(HouseListingProxy object1, HouseListingProxy object2) {
//            Date date1 = object1.getDueDate();
//            Date date2 = object2.getDueDate();
//            if (date1 != null) {
//                if (date2 != null) {
//                    return date1.compareTo(date2);
//                } else {
//                    return -1;
//                }
//            } else {
//                if (date2 != null) {
//                    return 1;
//                }
//            }
            return 0;
        }
    };

	public ResultListAdapter(Activity context, List<HouseListingProxy> items ) {
		this.context = context;
		this.listings = items;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	// static to save the reference to the outer class and to avoid access to
	// any members of the containing class
	static class ViewHolder {
		public ImageView imageView;
		public TextView listingName;
		public TextView listingAddress;
		public TextView listingPrice;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// ViewHolder will buffer the assess to the individual fields of the row
		// layout

		ViewHolder holder;
		// Recycle existing view if passed as parameter
		// This will save memory and time on Android
		// This only works if the base layout for all classes are the same
		View rowView = convertView;
		if (rowView == null) {
			//LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.resultlistlayout, null, true);
			holder = new ViewHolder();
			holder.listingName = (TextView) rowView.findViewById(R.id.listingName);
			holder.listingAddress = (TextView) rowView.findViewById(R.id.listingAddress);
			holder.listingPrice = (TextView) rowView.findViewById(R.id.listingPrice);
			holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
			rowView.setTag(holder);
		} else {
			holder = (ViewHolder) rowView.getTag();
		}
		
		HouseListingProxy listing = listings.get(position);

        holder.listingName.setText(listing.getListingName());
        holder.listingAddress.setText(listing.getListingAddress());
        holder.listingPrice.setText(listing.getListingPrice());
		holder.imageView.setImageResource(R.drawable.house);

		return rowView;
	}
	

	@Override
	public int getCount() {
		
		return listings.size();
	}

	@Override
	public HouseListingProxy getItem(int position) {
		return listings.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

}

